export function check_mobile(phone) {
    if (!(/^1[3456789]\d{9}$/.test(phone))) return false;
    else return true;
}

//let curTime = formatDate(new Date(), 'yyyy-MM-dd')
export function formatDate(myDate, fmt) {
    let o = {
        "M+": myDate.getMonth() + 1, // 月份
        "d+": myDate.getDate(), // 日
        "h+": myDate.getHours(), // 小时
        "m+": myDate.getMinutes(), // 分
        "s+": myDate.getSeconds(), // 秒
        "q+": Math.floor((myDate.getMonth() + 3) / 3), // 季度
        "S": myDate.getMilliseconds() // 毫秒
    };
    if (/(y+)/.test(fmt))
        fmt = fmt.replace(RegExp.$1, (myDate.getFullYear() + "").substr(4 - RegExp.$1.length));
    for (let k in o)
        if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
    return fmt;
}


export const monthDays = new class MonthDays {
    getDates(d, t = 0) {
        let curDate = new Date(d.replace(/-/g, '/'))
        let curMonth = curDate.getMonth()
        curDate.setMonth(curMonth + t + 1)
        curDate.setDate(0)
        let curDates = new Array(curDate.getDate()).fill(0).map((item, key) => {
            return key + 1
        })
        return curDates
    }

    getWeek(d) {
        let curDate = new Date(d.replace(/-/g, '/'))
        curDate.setDate(1)
        return curDate.getDay()
    }

    getFullChunkDates(d) {
        let curDates = this.getDates(d)
        let preDates = this.getDates(d, -1)
        let nexDates = this.getDates(d, 1)
        let curWeek = this.getWeek(d)
        curDates = curDates.map((i, k) => {
            return {num: i, fullDate: /(^\d{4})-(\d{1,2})-/.exec(d)[0] + i, show: 1}
        })
        preDates = preDates.map(i => {
            return {num: i, show: 0}
        })
        nexDates = nexDates.map(i => {
            return {num: i, show: 0}
        })
        let preCurDates = preDates.slice(preDates.length - (curWeek === 0 ? 6 : curWeek - 1), preDates.length).concat(curDates)
        return preCurDates.concat(nexDates.slice(0, 42 - preCurDates.length))
    }
}

export function getCurrentWeek(dateStr) {
    let date = dateStr ? new Date(dateStr) : new Date();
    console.log("getCurrentWeek", date)
    let beginDate = new Date(date.getFullYear(), 0, 1);
    let week = Math.ceil((parseInt((date - beginDate) / (24 * 60 * 60 * 1000)) + 1 + beginDate.getDay()) / 7);
    return week
}

export function getYearWeek() {
    let date = new Date(), lastDay = `${date.getFullYear()}-12-31`
    let totalWeek = getCurrentWeek(lastDay), currentWeek = getCurrentWeek()
    return {totalWeek, currentWeek}
}
